﻿using System;
using System.Text;

namespace ConsoleAppDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //string oldStr = "((Worksheet)this.workBook.Worksheets.get__Default(1)).Copy(this.missing, (Worksheet)this.workBook.Worksheets[this.workBook.Worksheets.Count]);";
            //string oldStr = "Cells.get__Default(num2, 2), this.workSheet.Cells.get__Default(num2, 16));";
            //string oldStr = "this.workSheet.Cells.set__Default(3, 12, sInvoiceCode);";
            //string oldStr = "this.workSheet.Cells.set__Default(num2, 1, dsInvoiceItem.Tables[0].Rows[i][j].ToString());";
            string oldStr = "this.range.set_Value2(this.range.Text.ToString().Replace(?, sOperatorName));";
           // string oldStr = "this.workSheet.set_Name((this.workSheet.Index - 1).ToString());";

            string newStr = Replaceset__Value2(oldStr);
            Console.WriteLine(newStr);
            Console.ReadLine();

        }

        static string Replaceget__Default(string oldStr)
        {
            string keyStr = ".get__Default";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStr[oldStrIndex] == ')')
                        {
                            newStr.Append("]");
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append("[");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }

        static string Replaceset__Default(string oldStr)
        {
            string keyStr = ".set__Default";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            int commaChIndex = 0;
            int commaChlastIndex = oldStr.LastIndexOf(')');
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStr[oldStrIndex] == ',')
                        {
                            commaChIndex++;
                            if(commaChIndex==1)
                            {
                                newStr.Append(oldStr[oldStrIndex]);
                            }
                            else
                            {
                                newStr.Append("]=");
                            }
                        }
                        else if(oldStrIndex == commaChlastIndex)
                        {
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append("[");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }

        static string Replaceset__Value2(string oldStr)
        {
            string keyStr = ".set_Value2";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            int commaChFristIndex = oldStr.IndexOf('(');
            int commaChlastIndex = oldStr.LastIndexOf(')');
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStrIndex == commaChFristIndex)
                        {
   
                        }
                        else if (oldStrIndex == commaChlastIndex)
                        {
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append(".Value2=");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }

        static string Replaceset_Name(string oldStr)
        {
            string keyStr = ".set_Name";
            if (!oldStr.Contains(keyStr)) return oldStr;
            StringBuilder newStr = new StringBuilder();
            int oldStrIndex = 0;
            bool isFindkey = false;
            int commaChFristIndex = oldStr.IndexOf('(');
            int commaChlastIndex = oldStr.LastIndexOf(')');
            while (oldStrIndex < oldStr.Length)
            {
                int keyStrIndex = 0;
                int tempoldStrIndex = oldStrIndex;
                while (keyStrIndex < keyStr.Length)
                {
                    if (oldStr[tempoldStrIndex] != keyStr[keyStrIndex]) break;
                    tempoldStrIndex++;
                    keyStrIndex++;
                }
                if (tempoldStrIndex == oldStrIndex
                    || keyStrIndex < keyStr.Length)
                {
                    //没匹配成功的情况
                    if (isFindkey)
                    {
                        if (oldStrIndex == commaChFristIndex)
                        {

                        }
                        else if (oldStrIndex == commaChlastIndex)
                        {
                            isFindkey = false;
                        }
                        else
                        {
                            newStr.Append(oldStr[oldStrIndex]);
                        }
                    }
                    else
                    {
                        newStr.Append(oldStr[oldStrIndex]);
                    }
                }
                else
                {

                    newStr.Append(".Name=");
                    isFindkey = true;
                    oldStrIndex = tempoldStrIndex;
                }
                oldStrIndex++;
            }


            return newStr.ToString();
        }
    }
}
